﻿var parkMSys = {};

parkMSys.$A = function (iterable) {
    
    if (navigator.userAgent.indexOf('AppleWebKit/') > -1) {
        if (!iterable) return [];
        if (!((typeof iterable == "function") && iterable == '[object NodeList]') &&
        iterable.toArray) return iterable.toArray();
        var length = iterable.length || 0, results = new Array(length);
        while (length--) results[length] = iterable[length];
        return results;
    }
    else {
        if (!iterable) return [];
        if (iterable.toArray) return iterable.toArray();
        var length = iterable.length || 0, results = new Array(length);
        while (length--) results[length] = iterable[length];
        return results;
    }
}

parkMSys.TryFunc = function () {
    if (arguments.length == 0) {
        return;
    }
    var tryTime = 300;
    var argArr = this.$A(arguments);
    var fname = argArr.shift();
    (function () {
        try {
            if (tryTime > 0) {
                if(typeof fname=="string")
                {
                    window[fname].apply(null, argArr);
                }
                else if(typeof fname == "function")
                {
                    fname.apply(null,argArr);
                }
            }
        }
        catch (e1) {
            tryTime--;
            setTimeout(arguments.callee, 100);
        }
    })();
};

parkMSys.TryFuncByCondition = function () {
    if (arguments.length < 2) {
        return;
    }
    var tryTime = 300;
    var argArr = this.$A(arguments);
    var condition = argArr.shift();
    var fname = argArr.shift();
    (function () {
        try {
            var excute = false;
            if (typeof condition == "function") {
                excute = condition.apply(null);
            }
            else if (typeof condition == "string") {                
                var f = new Function("return " + condition);
                excute = f();
            }
            if (tryTime > 0) {
                if (excute) {
                    if (typeof fname == "string") {
                        window[fname].apply(null, argArr);
                    }
                    else if (typeof fname == "function") {
                        fname.apply(null, argArr);
                    }
                }
                else {
                    tryTime--;
                    setTimeout(arguments.callee, 100);
                }
            }
        }
        catch (e1) {
            tryTime--;
            setTimeout(arguments.callee, 100);
        }
    })();
};

parkMSys.release = false;

var results = (/[\\\/]+([^\\\/]+)[\\\/]+([^\\\/\?#]+)[\?#]*[^\\\/]*$/gi).exec(window.location.href);

parkMSys.folder = results[1]; 

parkMSys.page = results[2];

var scriptElements = document.getElementsByTagName("script");
var script = scriptElements[scriptElements.length - 1];

parkMSys.parent = script.parentNode;

results = (/[^\?#&]+[\?#&]{1}callback=([^&]+)&*/gi).exec(script.src);
if (results) {
    parkMSys.callback = results[1];
}

results = (/[^\?#&]+[\?#&]{1}skin=([^&]+)&*/gi).exec(script.src);
if (results) {
    parkMSys.skin = results[1];
}
else {
    parkMSys.skin = "default";
}

results = (/[^\?#&]+[\?#&]{1}release=([^&]+)&*/gi).exec(script.src);
if (results) {
    parkMSys.release = !!(results[1]-0);
}

results = (/[\\\/]+load.js[\?\#]*/gi).exec(script.src);

parkMSys.jsSiteHost = script.src.substring(0, results.index);

parkMSys.loadJsRelyOnBaidu = function () {
    for (var i = 0; i < parkMSys.relyOnBaiduJs.length; i++) {
        parkMSys.loadJs(parkMSys.jsSiteHost + "/" + parkMSys.relyOnBaiduJs[i],null);
    }    
};

parkMSys.uaMatch = function () {
    // Useragent RegExp
    var rwebkit = /(webkit)[ \/]([\w.]+)/;
    var ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/;
    var rmsie = /(msie) ([\w.]+)/;
    var rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/;

    return (function (ua) {
        ua = ua.toLowerCase();

        var match = rwebkit.exec(ua) ||
			ropera.exec(ua) ||
			rmsie.exec(ua) ||
			ua.indexOf("compatible") < 0 && rmozilla.exec(ua) ||
			[];

        return { browser: match[1] || "", version: match[2] || "0" };
    })(navigator.userAgent);
}

parkMSys.loadCss = function (url) {
    var link = document.createElement("link");
    link.rel = "Stylesheet";
    link.type = "text/css";
    link.title = "parkMSys";
    link.href = url;
    parkMSys.parent.appendChild(link);
}

parkMSys.loadTemplate = function (url) {
    var s = document.createElement("script");
    s.src = parkMSys.jsSiteHost + "/load.aspx?callback=parkMSys.loadTemplateCallback&turl=" + encodeURIComponent(url);
    s.type = "text/javascript";
    parkMSys.parent.appendChild(s);
}

parkMSys.loadTemplateCallback = function (content) {
    var c = decodeURIComponent(content);
    var div = document.createElement("div");
    div.style.display = "none";
    div.innerHTML = c;
    document.body.appendChild(div);
}

parkMSys.changeSkin = function (skin) {

    var oldLinks = document.getElementsByTagName("link");

    var reg = new RegExp("[\\\/]+" + parkMSys.skin + "[\\\/]+", "gi");

    for (var i = 0; i < oldLinks.length; i++) {
        var l = oldLinks[i];
        if (l.title == "parkMSys") {

            l.href = l.href.replace(reg, "/"+skin+"/"); 
        }
    }

    parkMSys.skin = skin;
}

parkMSys.loadJs = function (url, load) {
    var s = document.createElement("script");
    s.src = url;
    s.type = "text/javascript";
    if (load && typeof load == "function") {
        var browser = parkMSys.uaMatch();
        if (browser.browser == "msie") {
            s.onreadystatechange = function () {
                if (this.readyState == 'loaded' || this.readyState == 'complete') {
                    load();
                }
            }
        }
        else {
            s.onload = load;
        }        
    }
    parkMSys.parent.appendChild(s);
}

parkMSys.loadJs(parkMSys.jsSiteHost + "/js/config/" + parkMSys.folder + "/" + parkMSys.page + (parkMSys.release ? ".release" : "") + ".js", function () {

    function isExclude(url) {
        if (url.indexOf("?e=1") != -1 || url.indexOf("&e=1") != -1) {
            return true;
        }
        return false;
    }

    var pageCssSrcArr = parkMSys.cssConfig;

    if (pageCssSrcArr) {
        for (var i = 0; i < pageCssSrcArr.length; i++) {
            var url = pageCssSrcArr[i];
            if (!(url.toLowerCase().indexOf("http") == 0)) {
                url = parkMSys.jsSiteHost + "/" + url;
                if (!parkMSys.release) {
                    url += "?" + Math.random();
                }
            }
            parkMSys.loadCss(url);
        }
    }

    var pageTemplateSrcArr = parkMSys.templateConfig;

    if (pageTemplateSrcArr) {
        for (var i = 0; i < pageTemplateSrcArr.length; i++) {
            var url = pageTemplateSrcArr[i];
            if (!(url.toLowerCase().indexOf("http") == 0)) {
                url = parkMSys.jsSiteHost + "/" + url;
            }
            parkMSys.loadTemplate(url);
        }
    }

    var isexcludedown = false;

    var pageJsSrcArr = parkMSys.jsConfig;
    if (pageJsSrcArr && pageJsSrcArr.length > 0) {
        var n = 0;
        (function () {
            var callee = arguments.callee;
            if (isexcludedown) {
                return;
            }
            if (n == pageJsSrcArr.length) {
                if (parkMSys.callback) {
                    parkMSys.TryFunc(parkMSys.callback);
                }
                return;
            }
            var url = pageJsSrcArr[n++];
            isexcludedown = isExclude(url);
            if (!(url.toLowerCase().indexOf("http") == 0)) {
                url = parkMSys.jsSiteHost + "/" + url;
                if (!parkMSys.release) {
                    if (!isexcludedown) {
                        url += "?" + Math.random();
                    }
                    else {
                        url += "&" + Math.random();
                    }
                }
            }

            if (isexcludedown) {
                parkMSys.loadJs(url, function () {
                    isexcludedown = false;
                    callee();
                });
            }
            else {
                parkMSys.loadJs(url);
                callee();
            }

        })();
    }
});

